<template>
  <div class="mod-prod prod-list-mod">
    <!-- 查询和其他操作 -->
    <div class="search-bar">

      <el-form :inline="true" label-width="auto" class="search-form" size="small">

        <div class="input-row">

          <el-form-item label="分类名">
            <el-input
              clearable
              @keyup.enter.native="handleFind"
              class="filter-item"
              style="width: 200px;"
              v-model="keyword"
              placeholder="请输入分类名"
            ></el-input>
          </el-form-item>
          <div class="default-btn primary-btn" @click="handleFind">{{$t('crud.searchBtn')}}</div>
          <div class="default-btn" @click="resetSearchForm()">{{$t('crud.reset')}}</div>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <el-row :gutter="10" style="margin-bottom: 8px;">
          <div
            class="default-btn"
            @click="handleAdd">
            添加分类
          </div>
          <div
            class="default-btn"
            @click="handleDeleteBatch">
            删除选中
          </div>
        </el-row>
      </div>

      <div class="table-con prod-table">
        <el-table :data="tableData"
                  style="width: 100%"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="序号" width="60" align="center">
            <template slot-scope="scope">
              <span>{{scope.$index + 1}}</span>
            </template>
          </el-table-column>

          <el-table-column label="分类名" width="100" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.sortName }}</span>
            </template>
          </el-table-column>

          <el-table-column label="分类介绍" width="250" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.content }}</span>
            </template>
          </el-table-column>

<!--          <el-table-column label="点击数" width="100" align="center" prop="clickCount" sortable="custom"-->
<!--                           :sort-by="['clickCount']">-->
<!--            <template slot-scope="scope">-->
<!--              <span>{{ scope.row.clickCount }}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->

          <el-table-column label="排序" width="100" align="center" prop="sort" >
            <template slot-scope="scope">
              <el-tag type="warning">{{ scope.row.sort }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="创建时间" width="160" align="center" prop="createTime" >
            <template slot-scope="scope">
              <span>{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>

          <el-table-column label="状态" width="100" align="center">
            <template slot-scope="scope">
              <template v-if="scope.row.status == 1">
                <span>正常</span>
              </template>
              <template v-if="scope.row.status == 2">
                <span>推荐</span>
              </template>
              <template v-if="scope.row.status == 0">
                <span>已删除</span>
              </template>
            </template>
          </el-table-column>

          <el-table-column label="操作" fixed="right" min-width="230">
            <template slot-scope="scope">
<!--              <el-button @click="handleStick(scope.row)" type="warning" size="small">置顶-->
<!--              </el-button>-->
              <el-button @click="handleEdit(scope.row)" type="primary" size="small">编辑
              </el-button>
              <el-button @click="handleDelete(scope.row)" type="danger" size="small">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>


    <!--分页-->
    <div class="block">
      <el-pagination
        v-if="tableData.length"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="分类名" :label-width="formLabelWidth" prop="sortName">
          <el-input v-model="form.sortName" auto-complete="off" maxlength="200"></el-input>
        </el-form-item>

        <el-form-item label="分类介绍" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.content" auto-complete="off" maxlength="1000"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
          <el-input v-model="form.sort" auto-complete="off" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
          <el-radio-group v-model="form.status" size="small">
            <el-radio :label="0">删除</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        multipleSelection: [], // 多选，用于批量删除
        tableData: [],
        keyword: '',
        currentPage: 1,
        pageSize: 10,
        total: 0, // 总数量
        title: '增加分类',
        dialogFormVisible: false, // 控制弹出框
        formLabelWidth: '120px',
        isEditForm: false,
        orderByDescColumn: '',
        orderByAscColumn: '',
        form: {
          uid: null,
          content: '',
          sortName: ''
        },
        rules: {
          sortName: [
            {required: true, message: '分类名称不能为空', trigger: 'blur'},
            {min: 1, max: 10, message: '长度在1到10个字符'}
          ],
          sort: [
            {required: true, message: '排序字段不能为空', trigger: 'blur'},
            {pattern: /^[0-9]\d*$/, message: '排序字段只能为自然数'}
          ]
        }
      }
    },
    created () {
      this.blogSortList()
    },
    methods: {
      // 从后台获取数据,重新排序
      changeSort (val) {
        // 根据当前排序重新获取后台数据,一般后台会需要一个排序的参数
        if (val.order == 'ascending') {
          this.orderByAscColumn = val.prop
          this.orderByDescColumn = ''
        } else {
          this.orderByAscColumn = ''
          this.orderByDescColumn = val.prop
        }
        this.blogSortList()
      },
      blogSortList: function () {
        var params = {}
        params.sortName = this.keyword

        // var params = {}
        // params.sortName = this.keyword
        // params.pageIndex = this.currentPage
        // params.pageSize = this.pageSize
        // params.orderByDescColumn = this.orderByDescColumn
        // params.orderByAscColumn = this.orderByAscColumn

        this.$http({
          url: this.$http.adornUrl('/admin/blogSort/page'),
          method: 'get',
          // params: this.$http.adornParams(params)
          params: this.$http.adornParams(
            Object.assign(
              {
                current: this.currentPage,
                size: this.pageSize,
                sortName: params.sortName
              },
            )
          )
        }).then(({ data }) => {
          this.tableData = data.records
          this.currentPage = data.current
          this.pageSize = data.size
          this.total = data.total
        })
      },
      getFormObject: function () {
        var formObject = {
          uid: null,
          content: null,
          sortName: null,
          sort: 0
        }
        return formObject
      },

      //搜索
      handleFind: function () {
        this.currentPage = 1
        this.blogSortList()
      },

      //重置表单加载初始数据
      resetSearchForm(){
        this.keyword = ""

        this.currentPage = 1
        this.blogSortList()
      },

      handleAdd: function () {
        this.title = '增加分类'
        this.dialogFormVisible = true
        this.form = this.getFormObject()
        this.isEditForm = false
      },
      // 通过点击量排序
      handleBlogSortByClickCount: function () {
        this.$confirm(
          '此操作将根据点击量对所有的标签进行降序排序, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(() => {
            blogSortByClickCount().then(response => {
              if (response.code == this.$ECode.SUCCESS) {
                this.$commonUtil.message.success(response.message)
                this.blogSortList()
              } else {
                this.$commonUtil.message.error(response.message)
              }
            })
          })
          .catch(() => {
            this.$commonUtil.message.info('已取消批量排序')
          })
      },
      // 通过点击量排序
      handleBlogSortByCite: function () {
        this.$confirm(
          '此操作将根据博客引用量对所有的标签进行降序排序, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(() => {
            blogSortByCite().then(response => {
              if (response.code == this.$ECode.SUCCESS) {
                this.$commonUtil.message.success(response.message)
                this.blogSortList()
              } else {
                this.$commonUtil.message.error(response.message)
              }
            })
          })
          .catch(() => {
            this.$commonUtil.message.info('已取消批量排序')
          })
      },
      handleEdit: function (row) {
        this.title = '编辑分类'
        this.dialogFormVisible = true
        this.isEditForm = true
        this.form = JSON.parse(JSON.stringify(row))
      },
      handleStick: function (row) {
        this.$confirm('此操作将会把该标签放到首位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            var params = {}
            params.uid = row.uid
            stickBlogSort(params).then(response => {
              if (response.code == this.$ECode.SUCCESS) {
                this.$commonUtil.message.success(response.message)
                this.blogSortList()
              } else {
                this.$commonUtil.message.error(response.message)
              }
            })
          })
          .catch(() => {
            this.$commonUtil.message.info('已取消置顶')
          })
      },
      handleDelete: function (row) {
        var that = this
        this.$confirm('此操作将把分类删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            var params = []
            params.push(row)
            that.deleteBatchBlogSort(params)
          })
          .catch(() => {
            that.$commonUtil.message.info('已取消删除')
          })
      },
      handleDeleteBatch: function () {
        var that = this
        if (that.multipleSelection.length <= 0) {
          that.$message.error('请先选中需要删除的内容!')
          return
        }
        that.$confirm('此操作将把选中的分类删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            that.deleteBatchBlogSort(that.multipleSelection)
          })
          .catch(() => {
            that.$commonUtil.message.info('已取消删除！')
          })
      },
      deleteBatchBlogSort: function (list) {
        var that = this
        var uids = list.map(item => {
          return item.uid
        })
        that.$http({
          url: that.$http.adornUrl(`/admin/blogSort/${uids}`),
          method: 'delete',
          data: that.$http.adornData(uids, false)
        }).then(response => {
          if (response.code == '00000') {
            that.$message({
              message: that.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                that.blogSortList()
              }
            })
          } else {
            that.$commonUtil.message.error(response.message)
          }
        })
      },
      handleCurrentChange: function (val) {
        this.currentPage = val
        this.blogSortList()
      },
      submitForm: function () {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            console.log('校验失败')
          } else {
            if (this.isEditForm) {
              this.$http({
                url: this.$http.adornUrl('/admin/blogSort'),
                method: 'put',
                data: this.$http.adornData(
                this.form)
              }).then(({ data }) => {
                this.$message({
                  message: this.$i18n.t('publics.operation'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.dialogFormVisible = false
                    this.blogSortList()
                  }
                })
              })
            } else {
              this.$http({
                url: this.$http.adornUrl('/admin/blogSort'),
                method: 'post',
                data: this.$http.adornData(
                this.form)
              }).then(({ data }) => {
                this.$message({
                  message: this.$i18n.t('publics.operation'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.dialogFormVisible = false
                    this.blogSortList()
                  }
                })
              })
            }
          }
        })
      },
      // 改变多选
      handleSelectionChange (val) {
        this.multipleSelection = val
      }
    }
  }
</script>
